﻿Option Explicit On
Option Strict On
Imports System.Text
Imports System.Data
Imports MySql.Data.MySqlClient
Imports System.Configuration
Public Class FrmLogin

    Dim Conn As New MySqlConnection()
    Dim com As New MySqlCommand()
    Dim dr As MySqlDataReader
    Dim dt As DataTable

    Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
        If MessageBox.Show("คุณต้องการออกจากโปรแกรมนี้ ใช่หรือไม่ ?", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
            Application.Exit()
        End If

    End Sub

    Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
        If txtUser.Text.Trim() = "" Or txtPassword.Text.Trim() = "" Then
            MessageBox.Show("กรุณากรอก Username หรือ Password ให้ครบด้วยครับ !!!", "คำเตือน", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Exit Sub
        End If
        com.CommandText = "SELECT * FROM staff WHERE Username='" & txtUser.Text.Trim() & "' AND Password='" & txtPassword.Text.Trim() & "'"
        com.Connection = Conn
        dr = com.ExecuteReader()
        If dr.HasRows() Then
            Dim frm As New FrmMainmenu()
            frm.Show()
            Me.Hide()
        Else
            MessageBox.Show("Username หรือ Password ไม่ถูกต้อง กรุณากรอกใหม่", "คำเตือน", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            txtUser.Focus()
            Exit Sub
        End If
    End Sub

    Private Sub FrmLogin_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim strConn As String = ConfigurationManager.ConnectionStrings("Equ").ConnectionString
        With Conn
            If .State = ConnectionState.Open Then .Close()
            .ConnectionString = strConn
            .Open()
        End With
    End Sub
End Class
